//
//  DDSeverSubViewController.m
//  PrivateCloud
//
//  Created by sarah doo on 13-10-14.
//  Copyright (c) 2013年 sarah doo. All rights reserved.
//

#import "DDLittleViewController.h"
#import "DDSeverTableCell.h"
#import "DDSuccessViewController.h"
#import "DDSeverTableModel.h"
#import "DDFileInfo.h"
#import "DDFolderInfo.h"
#import "MBProgressHUD.h"
#import "DDSuccessViewController.h"

@interface DDLittleViewController ()

@end

@implementation DDLittleViewController
@synthesize resultArray;
@synthesize titleName;
@synthesize localFolderInfo;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    isAllSelect = NO;
    
    self.resultArray = [[NSMutableArray alloc]init];
    for (DDFileInfo* fileInfo in localFolderInfo.files) {
        DDSeverTableModel *tableModel = [[DDSeverTableModel alloc]init];
        tableModel.titleName = [fileInfo fileName];
        tableModel.isSelected = NO;
        [resultArray addObject:tableModel];
        [tableModel release];
    }
    [self createTopView];
}


- (void)createTopView {
    UIView *linkView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 50)];
    linkView.backgroundColor = [UIColor greenColor];
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage *image = [UIImage imageNamed:@"ico_back.png"];
    backButton.frame = CGRectMake(10, 0, 48, 48);
    [backButton addTarget:self action:@selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [backButton setImage:image forState:UIControlStateNormal];
    [linkView addSubview:backButton];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2-25, 0, self.view.frame.size.width - image.size.width - 80, 50)];
    label.font = [UIFont systemFontOfSize:23];
    label.text = titleName;
    label.textColor = [UIColor whiteColor];
    [linkView addSubview:label];
    [label release];
    [self.view addSubview:linkView];
    
    UIButton *freshButton = [UIButton buttonWithType:UIButtonTypeCustom];
    freshButton.frame = CGRectMake(self.view.frame.size.width - 100, 0, 80, 50);
    freshButton.backgroundColor = [UIColor clearColor];
    UILabel *buttonLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, freshButton.frame.size.width, freshButton.frame.size.height)];
    buttonLabel.textColor = [UIColor whiteColor];
    buttonLabel.font = [UIFont systemFontOfSize:23];
    buttonLabel.backgroundColor = [UIColor clearColor];
    buttonLabel.text = @"全选";
    [freshButton addTarget:self action:@selector(selectAllItem:) forControlEvents:UIControlEventTouchUpInside];
    [freshButton addSubview:buttonLabel];
    [buttonLabel release];
    [linkView addSubview:freshButton];
    [linkView release];
    
    table = [[UITableView alloc] initWithFrame:CGRectMake( 5, 70, self.view.frame.size.width - 10, self.view.frame.size.height - 120) style:UITableViewStylePlain];
    table.delegate = self;
    table.dataSource = self;
    [self.view addSubview:table];
    //    [tableView release];
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - 50, self.view.frame.size.width, 50)];
    [footerView setBackgroundColor:[UIColor blackColor]];
    [self.view addSubview:footerView];
    
    UIButton *sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
    sureButton.frame = CGRectMake(self.view.frame.size.width/2, 0, 80, 50);
    sureButton.backgroundColor = [UIColor clearColor];
    UILabel *sureLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, freshButton.frame.size.width, freshButton.frame.size.height)];
    sureLabel.textColor = [UIColor whiteColor];
    sureLabel.font = [UIFont systemFontOfSize:23];
    sureLabel.backgroundColor = [UIColor clearColor];
    sureLabel.text = @"确   定";
    [sureButton addTarget:self action:@selector(sureButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    [sureButton addSubview:sureLabel];
    [sureLabel release];
    [footerView addSubview:sureButton];
    [footerView release];
}
//MARS 确定按钮的响应函数
- (void)sureButtonPressed:(id)sender {
    UIView *view = [[UIView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:view];
    view.backgroundColor = [UIColor blackColor];
    view.alpha = 0.8;
    view.userInteractionEnabled = NO;
    [self.view bringSubviewToFront:view];
    MBProgressHUD *progress = [MBProgressHUD showHUDAddedTo:view animated:YES];
    progress.mode = MBProgressHUDModeIndeterminate;
    progress.opacity = 0.6;
    progress.labelFont = [UIFont boldSystemFontOfSize:14];
    progress.detailsLabelFont = [UIFont boldSystemFontOfSize:12];
    progress.detailsLabelText =  @"正在下载数据..";
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.frame = CGRectMake(20, 20, 100, 50);
    [button setTitle:@"取消" forState:UIControlStateNormal];
    //MARS 取消按钮
    //[button ]
}


#pragma - mark tableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if ([resultArray count] > 0) {
        return [resultArray count];
    } else {
        return 2;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellID = @"cellID";
    DDSeverTableCell *cell = [[[DDSeverTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID] autorelease];
    cell.model = [self.resultArray objectAtIndex:indexPath.row];
    //    cell.nameLabel.text = @"test";
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    DDSeverTableModel *model = [self.resultArray objectAtIndex:indexPath.row];
    model.isSelected = !model.isSelected;
    [self.resultArray removeObjectAtIndex:indexPath.row];
    [self.resultArray insertObject:model atIndex:indexPath.row];
    [tableView reloadData];
}

- (void)backButtonPressed:(id)sender {
        DDSuccessViewController *sever = [[DDSuccessViewController alloc] initWithNibName:nil bundle:nil];
        [self presentViewController:sever animated:NO completion:nil];
        [sever release];

    
}
- (void)selectAllItem:(id)sender {
//    NSArray *array = [NSArray arrayWithArray:self.resultArray];
//    if (!isAllSelect) {
//        [self.resultArray removeAllObjects];
//        for (int i = 0; i < [array count]; i++) {
//            DDSeverTableModel *model = [array objectAtIndex:i];
//            model.isSelected = YES;
//            [self.resultArray addObject:model];
//        }
//        isAllSelect = YES;
//        [table reloadData];
//    } else {
//        [self.resultArray removeAllObjects];
//        for (int i = 0; i < [array count]; i++) {
//            DDSeverTableModel *model = [array objectAtIndex:i];
//            model.isSelected = NO;
//            [self.resultArray addObject:model];
//        }
//        isAllSelect = NO;
//        [table reloadData];
//    }
//    UILabel* buttonLable = view viewWithTag:
//    if(isAllSelect == YES){
//        //取消全选
//        for (DDSeverTableModel* model in self.resultArray) {
//            model.isSelected = NO;
//        }
//        isAllSelect = NO;
//    }
//    else{
//        //全选
//        for (DDSeverTableModel* model in self.resultArray) {
//            model.isSelected = YES;
//        }
//        isAllSelect = YES;
//    }
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
